package com.hup.winHolderService.constant;

import com.hup.winHolderService.framework.exception.AppHiddenMsgException;

import java.awt.event.InputEvent;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 鼠标按键
 *
 * @author hugan
 * @date 2023/10/10
 */
public enum MouseButton {
    LEFT(InputEvent.BUTTON1_DOWN_MASK, "左键"),
    RIGHT(InputEvent.BUTTON3_DOWN_MASK, "右键"),
    MIDDLE(InputEvent.BUTTON2_DOWN_MASK, "中键"),
    /**
     * 这两个键,不同鼠标可能不一样
     */
    BACKWARD(InputEvent.getMaskForButton(4), "后退"),
    FORWARD(InputEvent.getMaskForButton(5), "前进");

    public final int button;
    public final String desc;

    private static final List<String> codes = Arrays.stream(values())
            .map(mouseButton -> mouseButton.button + "")
            .collect(Collectors.toList());

    MouseButton(int button, String desc) {
        this.button = button;
        this.desc = desc;
        /*
        查询是否支持额外的鼠标按键
        boolean extraMouseButtonsEnabled = Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled();
        log.info("extraMouseButtonsEnabled={}", extraMouseButtonsEnabled);
        int numberOfButtons = MouseInfo.getNumberOfButtons();
        for (int i = 1; i <= numberOfButtons; i++) {
            int mask = InputEvent.getMaskForButton(i);

        }/**/
    }

    public static int getButton(String buttonStr) {
        if (!codes.contains(buttonStr)) {
            throw new AppHiddenMsgException("MouseButton异常:" + buttonStr);
        }
        return Integer.parseInt(buttonStr);
    }

}